package com.hzit.controller;

import com.hzit.entity.PageResult;
import com.hzit.entity.R;
import com.hzit.entity.Specification;
import com.hzit.entity.group.SpecGroup;
import com.hzit.service.SpecificationService;
import com.hzit.validator.AddGroup;
import com.hzit.validator.UpdateGroup;
import com.hzit.vo.SpecificationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
// @Controller
// @ResponseBody
@RestController
@RequestMapping("/specification")
public class SpecificationController {
	@Autowired
	private SpecificationService specificationService;

	//1. 查询所有的规格列表
	@GetMapping("/list")
	public R findAll(){
		System.out.println("specification.findAll()");
		return R.ok().put("specifications",specificationService.findAll());
	}

	//2. 分页查询
	// @GetMapping("findByPage")
	//相当于：
	@RequestMapping(value = "findByPage",method = RequestMethod.GET)
	public R findByPage(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
		PageResult<Specification> pr = specificationService.findByPage(page,pageSize);
		return R.ok().put("pr",pr);
	}
	//3. 条件查询带分页
	@PostMapping("/search")
	public R search(Integer page, Integer pageSize, @RequestBody(required = false) SpecificationVo vo){
		System.out.println("page = " + page);
		System.out.println("pageSize = " + pageSize);
		System.out.println("vo = " + vo);
		PageResult<Specification> pr = specificationService.search(page,pageSize,vo);
		return R.ok().put("pr",pr);
	}

	//4. 根据规格id查询规格名字及规格选项列表
	@GetMapping("/findById")
	public R findById(Long id){
		SpecGroup group = specificationService.findById(id);
		return R.ok().put("group",group);
	}

	//5. 添加规格及规格选项
	@PostMapping("/add")
	// public R add(@Valid @RequestBody SpecGroup group){ // @Valid注解不能进行分组校验
	public R add(@Validated(AddGroup.class) @RequestBody SpecGroup group){
		//5.1 存放出错信息的集合
		//Map<String,String> errors = new HashMap<>();   // key：出错的字段名 value: 出错的信息
		// if (result.hasErrors()) {
		// 	List<FieldError> fieldErrors = result.getFieldErrors();
		// 	for (FieldError fieldError : fieldErrors) {
		// 		String field = fieldError.getField();
		// 		errors.put(field,fieldError.getDefaultMessage());
		// 	}
		// 	return R.error().put("errors",errors);
		// }
		specificationService.add(group);
		return R.ok("添加规格成功！");
	}

	//6. 修改规格及规格选项
	@PostMapping("/update")
	public R update(@Validated(UpdateGroup.class) @RequestBody SpecGroup group){
		specificationService.update(group);
		return R.ok("修改规格成功！");
	}

	//7. 删除规格及规格选项
	@GetMapping("/delete")
	public R delete(Long[] ids){
		//System.out.println("ids = " + ids); // 23,24
		specificationService.delete(ids);
		return R.ok("删除规格成功！");
	}
}
